package com.example.campus_activity_backend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.campus_activity_backend.common.ErrorCode;
import com.example.campus_activity_backend.domain.Teacher;
import com.example.campus_activity_backend.exception.BusinessException;
import com.example.campus_activity_backend.service.TeacherService;
import com.example.campus_activity_backend.mapper.TeacherMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
* @author Hongxia
* @description 针对表【t_manager_teacher】的数据库操作Service实现
* @createDate 2023-11-17 14:37:56
*/
@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher>
    implements TeacherService {
    @Resource
    private TeacherMapper teacherMapper;

    @Override
    public long addTeacher(Teacher teacher) {
        //参数校验
        if(teacher == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"教师参数为空");
        }
        String teacherName = teacher.getTeacherName();
        if(teacherName == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"教师名称为空");
        }
        //添加数据库
        boolean saveResult = this.save(teacher);
        if(!saveResult){
            throw new BusinessException(ErrorCode.SYSTEM_ERROR,"添加教师失败");
        }
        Integer id = teacher.getId();
        return id;
    }

    @Override
    public List<Teacher> getTeacherByName(String teacherName) {
        //参数校验
        if(teacherName == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"教师名为空");
        }
        //查询数据库
        QueryWrapper<Teacher> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("teacher_name",teacherName);
        List<Teacher> teachers = teacherMapper.selectList(queryWrapper);
        if(teachers == null){
            throw new BusinessException(ErrorCode.NO_FOUND_ERROR,"没有该教师");
        }
        return teachers;
    }
}




